package com.ta.gulimall.auth.controller;

import com.ta.gulimall.auth.service.LoginService;
import com.ta.gulimall.auth.service.RegisterService;
import com.ta.gulimall.auth.view.vo.UserLoginVo;
import com.ta.gulimall.common.constant.AuthConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
public class LoginController {
    @Autowired
    LoginService LoginService;

    /**
     * 登录账号
     * @param vo
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String register(@Valid UserLoginVo vo, HttpSession session){
        LoginService.login(vo, session);

        return "redirect:http://gulimall.com";
    }

    /**
     * 跳转登录页面
     * @param session
     * @return
     */
    @GetMapping("/login.html")
    public String loginHtml(HttpSession session){
        if (session.getAttribute(AuthConstant.LOGIN_USER) != null) {
            return "redirect:http://gulimall.com";
        }

        return "login";
    }
}
